import { getToken, setToken, removeToken } from '@/utils/auth'
// 从req
import { login } from '@/api/user.js'

export default {
  // 每个模块必须开启命名空间
  namespaced: true,
  state: {
    // 项目刚刚开启就获取token
    token: getToken()
  },
  getters: {
  },
  mutations: {
    // 设置Token
    SET_TOKEN (state, val) {
      state.token = val
      setToken(val)
    },
    // 删除Token
    DEL_TOKEN (state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async login ({ commit }, data) {
      console.log(data)
      const re = await login({
        mobile: data.phone,
        password: data.password
      })
      // 存token
      commit('SET_TOKEN', re.data.data)
    }
  }
}
